﻿using System;
using System.Data;
using System.Text.RegularExpressions;

namespace Vcm
{
    public class LibraryCardData
    {
        public readonly string Name;
        public readonly string Type;
        public readonly string Clan;
        public readonly string Sphere;
        public readonly string Text;
        public readonly string FlavorText;
        public readonly string PoolCost;
        public readonly string BloodCost;
        public readonly string Illustrator;
        public readonly string Discipline;
        public readonly bool HasBurnOption;
        public int Copies;

        private static bool _getColumnCopies = true;

        public LibraryCardData(DataRow card)
        {
            Name = card["name"].ToString().Trim().Replace("(TM)", "™");
            Type = card["type"].ToString().Trim();
            Clan = card["clan"].ToString().Trim();
            Sphere = card.Table.Columns.Contains("sphere")
                ? card["sphere"].ToString().Trim()
                : string.Empty;
            Text = card["card text"].ToString().Trim().Replace("(TM)", "™");
            FlavorText = card["flavor text"].ToString().Trim().Replace("(TM)", "™");
            PoolCost = card["Pool cost"].ToString().Trim();
            BloodCost = card["Blood cost"].ToString().Trim();
            var artist = card["Artist"].ToString().Trim();
            var artists = artist.Split(';');
            Illustrator = artists[artists.Length - 1];
            Discipline = card["discipline"].ToString().Trim();
            var burnOption = card["burn option"].ToString().Trim();
            HasBurnOption = burnOption.Length > 0 && burnOption.StartsWith("y", StringComparison.InvariantCultureIgnoreCase);

            _getColumnCopies &= card.Table.Columns.Contains("Copies");
            Copies = _getColumnCopies ? int.Parse(card["Copies"].ToString()) : 1;
        }

        public bool IncreasesCapacity
        {
            get
            {
                return (Text.Contains("Master: Discipline") && Text.Contains("Capacity increases by 1"))
                    || Text.Contains("The vampire with this retainer increases his or her capacity by 1");
            }
        }

        public bool ReducesCapacity
        {
            get
            {
                return Text.Contains("This vampire's capacity is reduced by 1.");
            }
        }

        public bool HasCapacity
        {
            get
            {
                return Regex.IsMatch(Text, @"it represents a \d{1}-capacity", RegexOptions.IgnoreCase)
                    || Regex.IsMatch(Text, @"it becomes a \d{1}-capacity", RegexOptions.IgnoreCase)
                    || Regex.IsMatch(Text, @"it becomes a non-unique (\w+\s)+with \d{1} capacity", RegexOptions.IgnoreCase)
                    || Regex.IsMatch(Text, @"it becomes a non-unique \d{1}-capacity", RegexOptions.IgnoreCase)
                    || Regex.IsMatch(Text, @"It becomes a unique (\w+\s)+with \d{1} capacity", RegexOptions.IgnoreCase)
                    || Regex.IsMatch(Text, @"Capacity increases by \d{1}", RegexOptions.IgnoreCase);
            }
        }

        public string CorrectedName
        {
            get 
            { 
                const string the = ", The";
                return Name.EndsWith(the)
                    ? "The " + Name.Substring(0, Name.Length - the.Length)
                    : Name; 
            }
        }
    }
}